#include "AnaDef.h"
#include "XGLUtils.h"

void GetHists(TString fin, const TH1D *herr, TGraphErrors *& gmpv, TGraphErrors *& gsep, const Int_t style, const Int_t color)
{
  printf("\nFile: %s\n", fin.Data());
  TFile::Open(fin);
  //gDirectory->ls();

  fin = fin(fin.Last('/')+1,1000);

  TString gname=fin(fin.First('e')+1,100);
  gname = gname(0, gname.First('Q'));
  gname.Prepend("type");

  TString ytit;
  //ytit = "T_{track}";
  //ytit = "N_{cls}";
  //ytit = "Q_{x}/N_{cls}";

  if(fin.Contains("Q0"))
    ytit = "Q_{x}";
  else if(fin.Contains("Q1"))
    ytit = "1/(1/Q)_{x}";

  TString tag(fin);
  tag.ReplaceAll("out","");
  tag.ReplaceAll(".root","");

  //=================================================

  TH1D *hmpv=(TH1D*)gDirectory->Get(tag+"_mpv");
  TH1D *hsep=(TH1D*)gDirectory->Get(tag+"_mpvsep");
  if(!hmpv || !hsep){
    printf("no hist! %s\n", tag.Data()); exit(1);
  }

  gmpv = XGLUtils::HistToGraph(hmpv, herr, fin.Contains("type0Q")? 1.5 : -10);
  gmpv->SetMarkerStyle(style);
  gmpv->SetMarkerSize(0.5);
  gmpv->SetMarkerColor(color);
  gmpv->SetLineColor(color);
  gmpv->SetName(gname+"mpv");
  gmpv->SetTitle("");
  gmpv->GetYaxis()->SetTitle(ytit+" mean (a.u.)");
  gmpv->GetXaxis()->SetTitle("log_{10}#beta#gamma");
  gmpv->GetXaxis()->SetLimits(-0.5, 4.5);

  gsep = XGLUtils::HistToGraph(hsep, herr, fin.Contains("type0Q")? 1.5 : -10);
  gsep->SetMarkerStyle(style);
  gsep->SetMarkerSize(0.5);
  gsep->SetMarkerColor(color);
  gsep->SetLineColor(color);
  gsep->SetName(gname+"sep");
  gsep->SetTitle("");
  gsep->GetYaxis()->SetTitle(ytit+" separation");
  gsep->GetXaxis()->SetTitle("log_{10}#beta#gamma");
  gsep->GetXaxis()->SetLimits(-0.5, 4.5);
}

void docmp(const TString fin0, const TString fin1, const TString fin2, const TString fin3, const TString fin4)
{
  TFile::Open("/u/xlu/.task/dEdxCosmic/lookup.root");
  //gDirectory->ls();
  TCanvas *c1=(TCanvas*)gDirectory->Get("c1");
  
  TH1D *he0 = (TH1D*) c1->GetPrimitive("hlut");
  TH1D *herr=(TH1D*)he0->Clone("herr");
  herr->GetXaxis()->SetLimits(he0->GetXaxis()->GetXmin()-log10(0.105658), he0->GetXaxis()->GetXmax()-log10(0.105658));

  //=================================
  const TString fins[]={fin0, fin1, fin2, fin3, fin4};
  const TH1D* hes[]={herr, herr, 0x0, 0x0, 0x0};
  const Int_t cols[]={kGreen, kRed, kBlue, kMagenta, kBlack};
  const Int_t stys[]={22, 23, 20, 21, 29};

  const Int_t nfile = sizeof(fins)/sizeof(TString);
  TGraphErrors *gmpvs[nfile], *gseps[nfile];
  TString txt[nfile];
  for(Int_t ii=0; ii<nfile; ii++){
    GetHists(fins[ii], hes[ii], gmpvs[ii], gseps[ii], stys[ii], cols[ii]);

    if(fins[ii].Contains("type0Q"))
      txt[ii] = "#mu, TRD dE/dx (cosmic)";
    else if(fins[ii].Contains("type1Q"))
      txt[ii] = "#mu, TRD dE/dx+TR (cosmic)";
    else if(fins[ii].Contains("type2Q"))
      txt[ii] = "p, #pi, e, TRD dE/dx+TR (pp, #sqrt{s}=7 TeV)";
    else if(fins[ii].Contains("type3Q"))
      txt[ii] = "p, #pi, e, TRD dE/dx+TR (PbPb, #sqrt{s}=2.76 ATeV, all centralities)";
    else if(fins[ii].Contains("type4Q"))
      txt[ii] = "p, #pi, e, TRD dE/dx+TR (PbPb, #sqrt{s}=2.76 ATeV, Centrality 0-20%)";
    else{
      printf("type unknown! %d %s\n", ii, fins[ii].Data()); exit(1);
    }
  }

  TCanvas *cmpv = new TCanvas("cmpv"); 
  cmpv->SetGrid();
  TLegend *lmpv=new TLegend(0.15, 0.65, 0.85, 0.9);
  lmpv->SetFillStyle(0);
  lmpv->SetBorderSize(-1);
  const TString ytit = gmpvs[0]->GetYaxis()->GetTitle();
  if(ytit.Contains("Q")){
    gmpvs[0]->SetMaximum(2.5);
    gmpvs[0]->SetMinimum(0.9);
  }
  else if(ytit.Contains("T")){
    gmpvs[0]->SetMaximum( 0.3);
    gmpvs[0]->SetMinimum(-0.075);
  }
  else if(ytit.Contains("N")){
    gmpvs[0]->SetMaximum( 200);
    gmpvs[0]->SetMinimum( 80);
  }
  else{
    printf("ytit wrong! %s\n", ytit.Data());exit(1);
  }
  for(Int_t ii=0; ii<nfile; ii++){
    gmpvs[ii]->Draw(ii?"p":"ap"); 
    lmpv->AddEntry(gmpvs[ii], txt[ii], "p");
  }
  lmpv->Draw();

  TCanvas *csep = new TCanvas("csep");
  csep->SetGrid();
  TLegend *lsep=new TLegend(0.15, 0.65, 0.85, 0.9);
  lsep->SetFillStyle(0);
  lsep->SetBorderSize(-1);
  if(ytit.Contains("Q")){
    gseps[0]->SetMaximum(3.5);
    gseps[0]->SetMinimum(0);
  }
  else if(ytit.Contains("T")){
    gseps[0]->SetMaximum(1);
    gseps[0]->SetMinimum(0);
  }
  else if(ytit.Contains("N")){
    gseps[0]->SetMaximum(2);
    gseps[0]->SetMinimum(0);
  }

  for(Int_t ii=0; ii<nfile; ii++){
    gseps[ii]->Draw(ii?"p":"ap");
    lsep->AddEntry(gseps[ii], txt[ii], "p");
  }
  lsep->Draw();

  TFile *fout=new TFile(Form("CMPQ%d.root", fin0.Contains("Q1")),"recreate");
  herr->Write();
  for(Int_t ii=0; ii<nfile; ii++){
    gmpvs[ii]->Write();
    gseps[ii]->Write();
  }
  cmpv->Write();
  csep->Write();
  fout->Save();
  fout->Close();
}

int main(const int argc, char *argv[])
{
  for(int ii=0; ii<argc; ii++){
    printf("%d: %s\n", ii, argv[ii]);
  }

  if(argc!=6){
    printf("argc!=6\n");
    return 1;
  }

  docmp(argv[1], argv[2], argv[3], argv[4], argv[5]);

  return 0;
}
